package com.wondersgroup.app.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver;
import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver;

import com.wondersgroup.core.service.UtilService;




/**
 * 处理通讯中的客户端版本和设备号验证
 *
 * @author mengke
 */
public class FDAppRestInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(FDAppRestInterceptor.class);
    
    
    
    private StopWatch stopWatch = new StopWatch();
    
    private static final String TOKEN_CHECK_IGNORE_CHECK_PHONE_PATH = "/security/loginUser/checkPhoneNumAndSendSMS";
    
    private static final String TOKEN_CHECK_IGNORE_CHECK_CODE_PATH = "/security/loginUser/checkCode";
    
    private static final String TOKEN_CHECK_IGNORE_USER_ADD_PATH = "/security/loginUser/addLoginUser";
    
    private static final String TOKEN_CHECK_IGNORE_LOGIN_PATH = "/security/loginUser/login";
    
    private static final String TOKEN_CHECK_IGNORE_CHECK_PHONE_EXIST_PATH = "/security/loginUser/checkPhoneExist";
    
    private static final String TOKEN_CHECK_IGNORE_SEND_SMS_PATH = "/security/loginUser/sendSMSToChangePassword";
    
    private static final String TOKEN_CHECK_IGNORE_CHANGE_PASSWORD_PATH = "/security/loginUser/changePassword";
    
    
	@Autowired
	private UtilService utilService;

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
    	
    	
    	request.setAttribute("productType", null);
    	request.setAttribute("platformId", null);
    	request.setAttribute("isPlatformResponse", null);
        return true;
    }

    @Override
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
        try {
			//添加访问日志
			MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver();
			String methodName = methodNameResolver.getHandlerMethodName(request);
			
			String barcode = request.getParameter("barcode");
			Integer barcodeType = null;
			String batchId = request.getParameter("batchNo");
			Integer clientAppType = null;
			String clientAppVersion = null;
			String clientDeviceSn = request.getHeader("deviceSn");
			String serverIpAddress = request.getLocalAddr();
			String serverHostName = request.getLocalName();
			String requestName = methodName;
			String initialRequest = request.getRequestURI();
			String functionName = methodName;
			Integer productType = (Integer)request.getAttribute("productType");
			String gisLatitude = request.getHeader("gisLatitude");
			String gisLongitude = request.getHeader("gisLongitude");
			Integer platformId =(Integer)request.getAttribute("platformId");
			Integer isPlatformResponse =(Integer)request.getAttribute("isPlatformResponse");
			utilService.insertTraceHistory(barcode, barcodeType, batchId, clientAppType, clientAppVersion, clientDeviceSn, functionName, initialRequest,
					productType, requestName, serverHostName, serverIpAddress, gisLatitude,  gisLongitude,  platformId,  isPlatformResponse);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
        
    }

    @Override
    public void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        if (stopWatch.isRunning()) {
            stopWatch.stop();
        }
        logger.debug("total execution time(milliseconds):" + stopWatch.getTotalTimeMillis());
        logger.debug("~~~~~~~~ leave FDAppRestInterceptor ~~~~~~~~");
    }

    @Override
    public void afterConcurrentHandlingStarted(
            HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
    }
}
